package com.shop.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.shop.domain.User;
import com.shop.utils.JDBCUtils;
import com.shop.utils.MD5Util;

/**
 * 数据层
 * @author Administrator
 *
 */
public class UserDao {
	
	public User getUser(int id){
		try {
			String sql = "select * from users where id = ?";
			QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());
			return runner.query(sql, new BeanHandler<User>(User.class), id+"");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void insert(User user){
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = JDBCUtils.getConnection();
			String sql = "insert into users values(null,?,?,?,?,'user',0,?,?)";
			pst = conn.prepareStatement(sql);
			conn.setAutoCommit(false);
			pst.setObject(1, user.getUsername());
			pst.setObject(2, user.getPassword());
			pst.setObject(3, user.getNickname());
			pst.setObject(4, user.getEmail());
			pst.setObject(5, new Date());
			pst.setObject(6, user.getActivecode());
			int i=pst.executeUpdate();
			conn.commit();
			conn.setAutoCommit(false);
//			QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
//			Object[] params = {user.getUsername(), MD5Util.md5(user.getPassword()),user.getNickname(),user.getEmail(), new Date(), user.getActivecode()};
//			queryRunner.update(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("注册失败" + e.getMessage());
		} finally{
			JDBCUtils.close(null, pst, conn);
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
	public User getUser(String username){
		try {
			String sql = "select * from users where username = ?";
			QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
			ResultSetHandler rsh = new BeanHandler(User.class);   
			Object[] param = {username};
			return queryRunner.query(sql, param, rsh);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public User getUserByCode(String code){
		try {
			String sql = "select * from users where activecode = ?";
			QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
			return queryRunner.query(sql, new BeanHandler<User>(User.class), code);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 更新用户状态
	 * @param user
	 */
	public void updateState(User user) {
		try {
			String sql = "update users set state = ?  where id = ?";
			QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
			queryRunner.update(sql, user.getState(), user.getId());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("帐户激活失败");
		}
	}

	public User findUser(String username, String password) {
		try {
			String sql = "select * from users where username = ? and password = ?";
			QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
			//password = MD5Util.md5(password);
			return queryRunner.query(sql, new BeanHandler<User>(User.class), username, password);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}
